<?php

include_once './config.php';

/**
 * Represents a user's location.
 * @see User
 */
class Location {
	/**
	 * longitude
	 */
	public $lon;

	/**
	 * latitude
	 */
	public $lat;

	/**
	 * String representation of the given location.
	 */
	public $address;

	/**
	 * The EPOCH timestamp when the location was set.
	 */
	public $timestamp;

	/**
	 * The user entered note to this location
	 */
	public $data;


	/**
	 * Creates a new Location object.
	 * 
	 * @param string $lon [mandatory] longitude
	 * @param string $lat [mandatory] latitude
	 * @param int $timestamp [optional] timestamp when the location was set
	 * @param string $data [optional] user entered note to the location
	 * @param string $address [optional] address (human-readable position) got from Google geocode
	 * @return unknown_type
	 */
	public function __construct($lon, $lat, $timestamp = null, $data = null, $address = null) {
		$this->lon = $lon;
		$this->lat = $lat;
		$this->data = $data;
		$this->timestamp = (isset($timestamp) ? $timestamp : time());
		$this->address = $address;
	}

	/**
	 * Returns the string representation of the object.
	 * @return string
	 */
	public function toString() {
		return '[Location] lon=' . $this->lon . ', lat=' . $this->lat . ', timestamp=' . $this->timestamp . ', data=' . $this->data . ', address=' . $this->address;
	}

    /**
     * Retrieves a human-readable address at the given position (lon, lat)
     * @param $lon float the longitude value
     * @param $lat float the latitude value
     * @return Array the human-readable address at the passed location.
     *
     * @throws Exception
     * Errors:
     * - ERROR_GEOLOCATION_SERVER_UNAVAILABLE - if can't make connection to the geolocation server
     * - ERROR_GEOLOCATION_REQUEST_FAIL - if the geolocation in its response returns non-success (200) status code
     * - ERROR_GEOLOCATION_LONLAT_INVALID - if lon or lat is out of valid range
     */
    public static function getLocationAddress($lon, $lat) {
        // check the lon and lat values whether they are in the valid range
        if ($lon < -180 || $lon >180 || $lat < -90 || $lat > 90) {
            throw new Exception("Lon or lat is out of valid range!", ERROR_GEOLOCATION_LONLAT_INVALID);
        }

        // build up the query string
        $queryUrl = "http://maps.google.com/maps/geo?q=$lat,$lon&output=csv&oe=utf8&sensor=false";
        // open connection
        $file = fopen($queryUrl, "r");
        // if the connection failed
        if (!$file) {
            throw new Exception("Can't connect to georeverse server!", ERROR_GEOLOCATION_SERVER_UNAVAILABLE);
        }

        $locArr = fgetcsv($file);
        fclose($file);

        if ($locArr[0] != 200) { // bad HTTP error code?
            throw new Exception("A non-success status code got from the geolocation server!", ERROR_GEOLOCATION_REQUEST_FAIL);
        }

        return $locArr[2];
    }
}
?>
